package com.mzba.place.fragment;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;

import com.actionbarsherlock.app.SherlockFragment;
import com.google.gson.Gson;
import com.mzba.place.R;
import com.mzba.place.adapter.GridCardAdapter;
import com.mzba.place.db.SafeCardTable;
import com.mzba.place.entity.SafeCardInfo;
import com.mzba.place.entity.SafecardEntity;
import com.mzba.place.ui.MainActivity;
import com.mzba.place.ui.SafecardActiviy;
import com.mzba.place.utils.AppContext;
import com.mzba.place.utils.BasicUIEvent;
import com.mzba.place.utils.HttpUtils;
import com.mzba.place.utils.StringUtil;
import com.mzba.place.utils.UICore;
import com.mzba.place.utils.UrlConfig;

/**
 * 
 * @author 06peng
 *
 */
public class SafecardFragment extends SherlockFragment implements BasicUIEvent, Callback {
	
	private View view;
	private MainActivity mainActivity;
	private GridView gridView;
	private GridCardAdapter adapter;
	private List<SafecardEntity> safes;
	private Handler handler;
	
	private final int init_safecard_result = 1;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		handler = new Handler(this);
		mainActivity = (MainActivity) getSherlockActivity();
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.fragment_safe, null);
		gridView = (GridView) view.findViewById(R.id.safecard_grid);
		gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				Intent intent = new Intent(mainActivity, SafecardActiviy.class);
				intent.putExtra("md5", safes.get(arg2).getMd5());
				startActivityForResult(intent, init_safecard_result);
			}
		});
		return view;
	}
	
	@Override
	public void onResume() {
		super.onResume();
		UICore.eventTask(this, mainActivity, init_safecard_result, null, true);
	}
	
	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		switch (requestCode) {
		case init_safecard_result:
			UICore.eventTask(this, mainActivity, init_safecard_result, null, true);
			break;
		}
	}

	@Override
	public boolean handleMessage(Message msg) {
		adapter = new GridCardAdapter(mainActivity, safes);
		gridView.setAdapter(adapter);
		return false;
	}

	@Override
	public void execute(int mes, Object obj) {
		switch (mes) {
		case init_safecard_result:
			SafeCardTable st = new SafeCardTable(mainActivity);
			safes = st.get();
			if (safes != null && safes.isEmpty()) {
				getSafeCardFromServer();
				if (safes == null) {
					safes = new ArrayList<SafecardEntity>();
				}
			}
			SafecardEntity entity = new SafecardEntity();
			entity.setAll_count(0);
			entity.setAddress("");
			safes.add(entity);
			handler.sendEmptyMessage(init_safecard_result);
			break;
		}
	}
	
	private void getSafeCardFromServer() {
		try {
			String result = HttpUtils.doGet(UrlConfig.safe_cards + "?auth_token=" + AppContext.getInstance().getAuthToken());
			if (StringUtil.isNotBlank(result)) {
				Gson gson = new Gson();
				SafeCardInfo info = gson.fromJson(result, SafeCardInfo.class);
				if (info != null) {
					safes = info.getSafe_cards();
					if (safes != null && !safes.isEmpty()) {
						SafeCardTable st = new SafeCardTable(mainActivity);
						st.clearTable();
						for (SafecardEntity entity : safes) {
							st.save(entity);
						}
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
